package com.ruoyi.project.data.controller;

import java.security.Security;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.BasicCarUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.data.domain.Car;
import com.ruoyi.project.data.domain.TbTransportDriverCar;
import com.ruoyi.project.data.service.ITbTransportDriverCarService;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 人员车辆Controller
 *
 * @author ruoyi
 * @date 2020-03-23
 */
@RestController
@RequestMapping("/system/car")
public class TbTransportDriverCarController extends BaseController
{
    @Autowired
    private ITbTransportDriverCarService tbTransportDriverCarService;

    /**
     * 查询人员车辆列表
     */
    @PreAuthorize("@ss.hasPermi('weight:car:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbTransportDriverCar tbTransportDriverCar)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        tbTransportDriverCar.setRole(user.getRoles().get(0).getRoleKey());
        startPage();
        List<TbTransportDriverCar> list = tbTransportDriverCarService.selectTbTransportDriverCarList(tbTransportDriverCar);
        return getDataTable(list);
    }

    /**
     * 导出人员车辆列表
     */
    @PreAuthorize("@ss.hasPermi('weight:car:export')")
    @Log(title = "人员车辆", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbTransportDriverCar tbTransportDriverCar)
    {
        List<TbTransportDriverCar> list = tbTransportDriverCarService.selectTbTransportDriverCarList(tbTransportDriverCar);
        ExcelUtil<TbTransportDriverCar> util = new ExcelUtil<TbTransportDriverCar>(TbTransportDriverCar.class);
        return util.exportExcel(list, "car");
    }

    /**
     * 获取人员车辆详细信息
     */
    @GetMapping(value = "/{carId}")
    public AjaxResult getInfo(@PathVariable("carId") Long carId)
    {
        TbTransportDriverCar tbTransportDriverCar = tbTransportDriverCarService.selectTbTransportDriverCarById(carId);
        String carModel = tbTransportDriverCarService.getModel(tbTransportDriverCar.getUserIdCard(), tbTransportDriverCar.getCarLicenceNumber());
        if (StringUtils.isNotBlank(carModel)){
            String model = BasicCarUtils.getRealModel(carModel);
            Car car = tbTransportDriverCarService.getCar(model);
            if (car == null){
                car = BasicCarUtils.getBasicCar(model);
                if (car != null){
                    tbTransportDriverCarService.insertCar(car);
                }
            }
            tbTransportDriverCar.setCar(car);
        }
        return AjaxResult.success(tbTransportDriverCar);
    }

    /**
     * 新增人员车辆
     */
    @PreAuthorize("@ss.hasPermi('weight:car:add')")
    @Log(title = "人员车辆", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbTransportDriverCar tbTransportDriverCar)
    {
        return toAjax(tbTransportDriverCarService.insertTbTransportDriverCar(tbTransportDriverCar));
    }

    /**
     * 修改人员车辆
     */
    @PreAuthorize("@ss.hasPermi('weight:car:edit')")
    @Log(title = "人员车辆", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbTransportDriverCar tbTransportDriverCar)
    {
        return toAjax(tbTransportDriverCarService.updateTbTransportDriverCar(tbTransportDriverCar));
    }

    /**
     * 删除人员车辆
     */
    @PreAuthorize("@ss.hasPermi('weight:car:remove')")
    @Log(title = "人员车辆", businessType = BusinessType.DELETE)
	@DeleteMapping("/{carIds}")
    public AjaxResult remove(@PathVariable Long[] carIds)
    {
        return toAjax(tbTransportDriverCarService.deleteTbTransportDriverCarByIds(carIds));
    }

    /**
     * 获取识别车辆
     * @param tbTransportDriverCar
     * @return
     */
    @GetMapping("/identifyCarlist")
    public TableDataInfo identifyCarlist(TbTransportDriverCar tbTransportDriverCar)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        tbTransportDriverCar.setRole(user.getRoles().get(0).getRoleKey());
        startPage();
        List<TbTransportDriverCar> list = tbTransportDriverCarService.selectIdentifyCarList(tbTransportDriverCar);
        return getDataTable(list);
    }

    /**
     * 获取识别车辆详细信息
     */
    @GetMapping(value = "getIdentifyCarInfo/{carId}")
    public AjaxResult getIdentifyCarInfo(@PathVariable("carId") Long carId)
    {
        TbTransportDriverCar tbTransportDriverCar = tbTransportDriverCarService.selectIdentifyCarById(carId);
        return AjaxResult.success(tbTransportDriverCar);
    }

    /**
     * 修改识别车辆
     */
    @Log(title = "识别车辆", businessType = BusinessType.UPDATE)
    @PutMapping("/updateIdentifyCar")
    public AjaxResult updateIdentifyCar(@RequestBody TbTransportDriverCar tbTransportDriverCar)
    {
        if (StringUtils.isBlank(tbTransportDriverCar.getModel())){
            return AjaxResult.error("车辆品牌型号不能为空");
        }
        String model = BasicCarUtils.getRealModel(tbTransportDriverCar.getModel());
        Car car = tbTransportDriverCarService.getCar(model);
        if (car == null){
           return AjaxResult.error("标准中暂无该车型，请添加");
        }
        tbTransportDriverCar.setCarAxleNums(car.getAxleNumber());
        tbTransportDriverCar.setSource("2");
        String allModel = tbTransportDriverCar.getModel().split(model)[0] + car.getModel();
        if (StringUtils.isBlank(allModel)){
            allModel = car.getModel();
        }
        int i = tbTransportDriverCarService.updateModel(allModel, tbTransportDriverCar.getVin(), tbTransportDriverCar.getUserIdCard(), tbTransportDriverCar.getCarLicenceNumber());
        return toAjax(tbTransportDriverCarService.updateTbTransportDriverCar(tbTransportDriverCar));
    }

    /**
     * 新增基本车型
     */
    @Log(title = "基本车型", businessType = BusinessType.INSERT)
    @PostMapping("/addBatisCar")
    public AjaxResult addBatisCar(@RequestBody Car car)
    {
        Car car1 = tbTransportDriverCarService.getCar(car.getModel());
        if (car1 != null){
            return AjaxResult.error("标准中已存在该车型");
        }
        return toAjax(tbTransportDriverCarService.insertCar(car));
    }


}
